package com.csun.cmny.provider.config;

import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.vo.LifeRoomBuildingVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.service.DmcRpcService;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.RoomService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-15 20:12
 * @Description:
 */
@Slf4j
@Component
@Order(value = 1)
public class RedisInitRunner implements CommandLineRunner {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private OldmanService oldmanService;

    @Resource
    private RoomService roomService;

    @Resource
    private DmcRpcService dmcRpcService;
//    @Resource
//    private OmcLifeRoomService omcLifeRoomService;

    @Override
    public void run(String... args) throws Exception {

        log.info(">>>>>>>>>>>>>>> cmny-provider-omc loading redis data...  <<<<<<<<<<<<<");

        List<OldmanBedRoomBuildingVo> oldmanBedRoomBuildingVoList =
                oldmanService.queryOldmanBedRoomBuildingList(Lists.newArrayList());
        loadOldman(oldmanBedRoomBuildingVoList);
        log.info(">>>>>>>>>>>>>>> cmny-provider-omc load redis data oldman finish.  <<<<<<<<<<<<<");

        List<OldmanBedRoomBuildingVo> bedRoomBuildingOldmanVoList =
                oldmanService.queryBedRoomBuildingOldmanList(Lists.newArrayList());
        loadBed(bedRoomBuildingOldmanVoList);
        log.info(">>>>>>>>>>>>>>> cmny-provider-omc load redis data bed finish.  <<<<<<<<<<<<<");

        List<RoomBuildingVo> roomBuildingVoList =
                roomService.queryRoomBuildingList(Lists.newArrayList());
        loadRoom(roomBuildingVoList);
        log.info(">>>>>>>>>>>>>>> cmny-provider-omc load redis data room finish.  <<<<<<<<<<<<<");

//        List<LifeRoomBuildingVo> lifeRoomBuildingVoList = omcLifeRoomService.queryRoomBuildingList(Lists.newArrayList());
//        loadLifeRoom(lifeRoomBuildingVoList);
//        log.info(">>>>>>>>>>>>>>> cmny-provider-omc load redis data life room finish.  <<<<<<<<<<<<<");
    }

    public void refreshOldman(Integer oldmanId) {
        List<OldmanBedRoomBuildingVo> oldmanBedRoomBuildingVoList =
                oldmanService.queryOldmanBedRoomBuildingList(Lists.newArrayList(oldmanId));

        if (PublicUtil.isNotEmpty(oldmanBedRoomBuildingVoList) && oldmanBedRoomBuildingVoList.size() > 0) {
            OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = oldmanBedRoomBuildingVoList.get(0);
            List<String> tagList = Lists.newArrayList(DmcConstant.GROUP_PREFIX + oldmanBedRoomBuildingVo.getHomeId()
                    + "_" + oldmanBedRoomBuildingVo.getBuildingId()
                    + "_" + oldmanBedRoomBuildingVo.getFloorNo());
            sendBuildingFloorUpdateMsg(tagList);
        }

        //loadOldman(oldmanBedRoomBuildingVoList);
        if (PublicUtil.isNotEmpty(oldmanBedRoomBuildingVoList.get(0).getBedId())) {
            refreshBed(oldmanBedRoomBuildingVoList.get(0).getBedId());
        } else {
            loadOldman(oldmanBedRoomBuildingVoList);
        }
    }

    public void refreshBed(Integer bedId) {
        List<OldmanBedRoomBuildingVo> bedRoomBuildingOldmanVoList =
                oldmanService.queryBedRoomBuildingOldmanList(Lists.newArrayList(bedId));

        if (PublicUtil.isNotEmpty(bedRoomBuildingOldmanVoList) && bedRoomBuildingOldmanVoList.size() > 0) {
            OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = bedRoomBuildingOldmanVoList.get(0);
            List<String> tagList = Lists.newArrayList(DmcConstant.GROUP_PREFIX + oldmanBedRoomBuildingVo.getHomeId()
                    + "_" + oldmanBedRoomBuildingVo.getBuildingId()
                    + "_" + oldmanBedRoomBuildingVo.getFloorNo());
            sendBuildingFloorUpdateMsg(tagList);
        }

        if (bedRoomBuildingOldmanVoList.get(0).getOldmanId() != null) {
            loadOldman(bedRoomBuildingOldmanVoList);
        }

        loadBed(bedRoomBuildingOldmanVoList);
    }

    public void refreshRoom(Integer roomId) {
        List<RoomBuildingVo> roomBuildingVoList =
                roomService.queryRoomBuildingList(Lists.newArrayList(roomId));

        if (PublicUtil.isNotEmpty(roomBuildingVoList) && roomBuildingVoList.size() > 0) {
            RoomBuildingVo roomBuildingVo = roomBuildingVoList.get(0);
            List<String> tagList = Lists.newArrayList(DmcConstant.GROUP_PREFIX + roomBuildingVo.getHomeId()
                    + "_" + roomBuildingVo.getBuildingId()
                    + "_" + roomBuildingVo.getFloorNo());
            sendBuildingFloorUpdateMsg(tagList);
        }

        loadRoom(roomBuildingVoList);
    }

    public void refreshRoomBed(Integer roomId) {
        List<OldmanBedRoomBuildingVo> bedRoomBuildingOldmanVoList =
                oldmanService.queryBedRoomBuildingOldmanListByRoomId(roomId);
        loadBed(bedRoomBuildingOldmanVoList);

    }

    public void loadOldman(List<OldmanBedRoomBuildingVo> oldmanBedRoomBuildingVoList) {
        oldmanBedRoomBuildingVoList.forEach(o-> {
            redisTemplate.opsForValue().set(RedisKeyUtil.getOmcOldmanId(o.getOldmanId()), o);
        });
    }

    public void loadBed(List<OldmanBedRoomBuildingVo> bedRoomBuildingOldmanVoList) {
        bedRoomBuildingOldmanVoList.forEach(b->{
            redisTemplate.opsForValue().set(RedisKeyUtil.getOmcBedId(b.getBedId()), b);
        });
    }

    public void loadRoom(List<RoomBuildingVo> roomBuildingVoList) {
        roomBuildingVoList.forEach(r->{
            redisTemplate.opsForValue().set(RedisKeyUtil.getOmcRoomId(r.getRoomId()), r);
        });
    }

//    public void loadLifeRoom(List<LifeRoomBuildingVo> roomBuildingVoList) {
//        roomBuildingVoList.forEach(r->{
//            redisTemplate.opsForValue().set(RedisKeyUtil.getOmcLifeRoomId(r.getRoomId()), r);
//        });
//    }

    public void sendBuildingFloorUpdateMsg(List<String> tagList) {

        dmcRpcService.sendBuildingFloorUpdateMsg(tagList);
    }

}
